package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.union.api.common.model.ApiInterfaceFolder;
import com.gitee.zycra.union.api.common.vo.InterfaceFolderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zycra
 */
@Repository
public class ApiInterfaceFolderDao extends BaseDao<ApiInterfaceFolder> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 添加目录
     *
     * @param apiInterfaceFolder 目录对象
     * @return 影响行数
     */
    public int insert(ApiInterfaceFolder apiInterfaceFolder) {
        return insertIntoTable(apiInterfaceFolder);
    }

    /**
     * 根据ID更新目录
     *
     * @param apiInterfaceFolder 目录对象
     * @return 影响行数
     */
    public int update(ApiInterfaceFolder apiInterfaceFolder) {
        return updateTableById(apiInterfaceFolder);
    }

    /**
     * 删除目录
     *
     * @param id 目录ID
     * @return 影响行数
     */
    public int updateToDelete(Long id) {
        return updateTableById(new ApiInterfaceFolder().setId(id).setDeleted(true));
    }

    /**
     * 根据项目ID查询目录
     *
     * @param projectId 项目ID
     * @return 目录集合
     */
    public List<InterfaceFolderVO> selectByProjectId(Long projectId) {
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT id, folder_name FROM api_interface_folder WHERE project_id = ? AND deleted = 0",
                InterfaceFolderVO.class, List.of(projectId));
    }
}
